<template>
  <div>
    <el-dialog
      title="编辑用户"
      width="477px"
      center
      @close="cancelEvent"
      :visible.sync="isShow"
    >
      <el-form ref="form" :rules="rules" :model="form">
        <el-form-item label="用户名" label-width="100px" prop="username">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" label-width="100px" prop="email">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" label-width="100px" prop="phone">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色" label-width="100px" prop="role_id">
          <el-select v-model="form.role_id" placeholder="请选择">
            <el-option
              v-for="(value, key, index) in roleObj"
              :key="index"
              :label="value"
              :value="key"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" label-width="100px" prop="status">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option label="启用" value="0"></el-option>
            <el-option label="禁用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户备注" label-width="100px" prop="remark">
          <el-input v-model="form.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelEvent">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { userEdit } from '@/api/userList.js'
import { validatePhone, validateEmail } from '@/utils/validate.js'
export default {
  data () {
    return {
      isShow: false,
      dialogTableVisible: false,
      dialogFormVisible: false,
      roleObj: {
        1: '超级管理员',
        2: '管理员',
        3: '老师',
        4: '学生'
      },
      form: {
        username: '',
        email: '',
        phone: '',
        role_id: '',
        status: '',
        remark: ''
      },
      // 表单验证
      rules: {
        username: [{ required: true, message: '必填', trigger: 'change' }],
        email: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              validateEmail(value)
                ? callback()
                : callback(new Error('请输入正确的邮箱地址'))
            }
          }
        ],
        // 验证手机号
        phone: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              validatePhone(value)
                ? callback()
                : callback(new Error('请正确输入手机号'))
            }
          }
        ],
        role_id: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  methods: {
    cancelEvent () {
      this.isShow = false
    },
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          await userEdit(this.form)
          this.$message.success('编辑成功')
          this.isShow = false
          this.$emit('getData')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .el-dialog__header {
  padding: 20px 20px 10px;
  background-color: #0da2fa;
  .el-dialog__title{
    color: white;
  }
}
</style>
